/**
 * 添加或者修改页面
 */
var StoreAccountInfoDlg = {
    data: {
        storeId: "",
        storeName: "",
        account: "",
        remainingSum: "",
        createTime: "",
        createBy: "",
        updateTime: "",
        updateBy: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var data = data.field;
        if(data.storeId == ""){
            Feng.error("门店不能为空！");
            return false;
        }
        if (data.account == ""){
            Feng.error("账户不能为空！");
            return false;
        }
        var ajax = new $ax(Feng.ctxPath + "/storeAccount/addItem", function (data) {
            console.log(data);
            if (data.code == 500) {
                Feng.error(data.message);
                return false;
            }
            Feng.success("添加成功！");
            window.location.href = Feng.ctxPath + '/storeAccount'
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });

        ajax.set(data);
        ajax.start();

        return false;
    });

    form.on('select(warehouseId)', function (data) {
        // data.value 为当前选中的 value
        var selectedId = data.value;
        var $storeSel   = $('#storeId');
        console.log('选中的仓库 id：', selectedId);

        // 这里写你要执行的逻辑，比如发送 Ajax
        $.ajax({
            url: '/basicPutgoods/getStoreByWarehouseId?page=1&limit=100',
            type: 'GET',
            data: { warehouseId: selectedId },
            success: function (res) {
                console.log(res);
                storeList = res.data
                // 处理返回结果

                var html = '<option value="">请选择门店</option>';
                layui.each(storeList, function (index, item) {
                    html += '<option value="' + item.id + '">' + item.putgoodsName + '</option>';
                });
                $storeSel.html(html);
                form.render('select');   // 重新渲染下拉框
            }
        });
    });

    // form.on('select(storeId)', function (data) {
    //     // data.value 为当前选中的 value
    //     var selectedId = data.value;
    //     var $storeSel   = $('#storeId');
    //     let id = selectedId.split("7Y-5Y-2D-7S")[0];
    //     let name = selectedId.split("7Y-5Y-2D-7S")[1];
    //     console.log('选中的仓库 id：', id);
    //     $storeSel.val(id);
    // });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/storeAccount'
    });


});